﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace OperationPlugins.Validators
{
    /// <summary>
    /// A validation result message.
    /// </summary>
    [DebuggerDisplay("{Message}")]
    [Serializable]
    public class Message : MarshalByRefObject, ILogicalThreadAffinative
    {
        private readonly ValidationLevel _level;
        private readonly string _text;

        /// <summary>
        /// Gets the validation level of this message.
        /// </summary>
        public virtual ValidationLevel ValidationLevel
        {
            get { return _level; }
        }

        /// <summary>
        /// Gets the message text.
        /// </summary>
        public virtual string Text
        {
            get { return _text; }
        }

        /// <summary>
        /// Initializes a new instance of the Message class.
        /// </summary>
        /// <param name="level">The validation level</param>
        /// <param name="text">The message text</param>
        public Message(ValidationLevel level, string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            _level = level;
            _text = text;
        }

        /// <summary>
        /// Is this an error message.
        /// </summary>
        public virtual bool IsError
        {
            get { return ValidationLevel == ValidationLevel.Level1 || ValidationLevel == ValidationLevel.Level2; }
        }

        /// <summary>
        /// Is this a notification message.
        /// </summary>
        public virtual bool IsNotification
        {
            get { return ValidationLevel == ValidationLevel.Level3; }
        }

        /// <summary>
        /// Converts validation result messages to string.
        /// </summary>
        /// <param name="messages">Validation result messages.</param>
        /// <returns>The string representation of validation result messages.</returns>
        public static string ToString(IEnumerable<Message> messages)
        {
            if (messages == null)
            {
                throw new ArgumentNullException("messages");
            }

            var s = new StringBuilder();

            foreach (string message in ToStrings(messages))
            {
                s.AppendLine(message);
            }

            return s.ToString();
        }

        /// <summary>
        /// Converts validation result messages to strings.
        /// </summary>
        /// <param name="messages">Validation result messages.</param>
        /// <returns>String representations of validation result messages.</returns>
        public static IEnumerable<string> ToStrings(IEnumerable<Message> messages)
        {
            if (messages == null)
            {
                throw new ArgumentNullException("messages");
            }

            foreach (Message message in messages)
            {
                yield return message.Text;
            }
        }
    }
}